home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail.cf / src / pathsend / RCS / pathsend.c,v next >
Encoding:
Text File  |  1987-01-06  |  4.2 KB  |  239 lines

  1. head     1.2;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.2
  9. date     87.01.06.12.57.38;  author dudek;  state Exp;
  10. branches ;
  11. next     1.1;
  12.  
  13. 1.1
  14. date     86.01.10.15.05.04;  author dudek;  state Exp;
  15. branches ;
  16. next     ;
  17.  
  18.  
  19. desc
  20. @@
  21.  
  22.  
  23. 1.2
  24. log
  25. @*New* and *improved* - use '!' syntax for input addresses instead of '@@' -gd
  26. @
  27. text
  28. @/*
  29.  * pathsend dest-host user
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <ctype.h>
  34.  
  35. typedef struct {
  36.     char *dptr;
  37.     int dsize;
  38. } datum;
  39.  
  40. /* From sendmail sysexits.c */
  41. #define    ERR_USAGE    64
  42. #define    ERR_DATAERR    65
  43. #define    ERR_NOHOST    68
  44. #define    ERR_UNAVAILABLE    69
  45. #define    ERR_SOFTWARE    70
  46. #define    ERR_OSERR    71
  47. #define    ERR_OSFILE    72
  48.  
  49. extern datum fetch();
  50.  
  51. #define PATHALIASES    "/usr/lib/pathaliases"
  52. #define SENDMAIL    "/usr/lib/sendmail"
  53.  
  54. #define MAXRECIPIENTS    100+3
  55.  
  56. char *sendmail = SENDMAIL;
  57.  
  58. main(argc, argv)
  59. char *argv[];
  60. {
  61.     datum key, content;
  62.     int i, pid, verbose = 0, debug = 0, index, saveindex;
  63.     register char *bp;
  64.     char *user, *from = ((char *) NULL), *mkstring(), *findhost();
  65.     char dest[2048], buf[2048];
  66.     char *sendargv[MAXRECIPIENTS];
  67.  
  68.     for (argc--, argv++; argc && **argv == '-'; argc -= i, argv += i) {
  69.     i = 1;
  70.     while (*++*argv) {
  71.         switch (**argv) {
  72.         case 'd':
  73.             debug++;
  74.             break;
  75.         case 'f':
  76.             if (argc <= i)
  77.             done(ERR_USAGE,"Not enough args for '-f'\n");
  78.             from = argv[i++];
  79.             break;
  80.         case 'm':
  81.             if (argc <= i)
  82.             done(ERR_USAGE,"Not enough args for '-m'\n");
  83.             sendmail = argv[i++];
  84.             break;
  85.         case 'v':
  86.             verbose++;
  87.             break;
  88.         default:
  89.             fprintf(stderr,"Unknown switch '-%c'\n",**argv);
  90.             done(ERR_USAGE,
  91.         "Usage: pathsend [-dv] [-m mailer] [host!...!user]\n");
  92.         }
  93.     }
  94.     }
  95.  
  96.     if (dbminit(PATHALIASES) < 0)
  97.     done(ERR_SOFTWARE,"pathsend: can't dbminit %s\n",PATHALIASES);
  98.  
  99.     index = 0;
  100.     sendargv[index++] = "pathsendmail";
  101.  
  102.     if (verbose)
  103.     sendargv[index++] = "-v";
  104.     
  105.     if (from) {
  106.     sendargv[index++] = "-f";
  107.     sendargv[index++] = from;
  108.     }
  109.  
  110.     saveindex = index;
  111.     for (; argc > 0; argv++, argc--) {
  112.  
  113.     /* Default pathalias address is original address */
  114.     strcpy(buf,*argv);
  115.  
  116.     /* Try and pathalias each host in left-to-right order,
  117.      * until a pathalias-able host is found
  118.      */
  119.     for (user = *argv; user = findhost(user,dest);) {
  120.  
  121.         key.dptr = dest;
  122.         key.dsize = strlen(dest) + 1;
  123.         content = fetch(key);
  124.  
  125.         if (content.dptr != 0) {
  126.         sprintf(buf, content.dptr, user);
  127.         break;
  128.         }
  129.     }
  130.  
  131.     /* Make sure there is no recursive pathaliasing */
  132.     strcat(buf,";nopath");
  133.  
  134.     sendargv[index++] = mkstring(buf);
  135.     }
  136.  
  137.     if (index != saveindex) {
  138.     sendargv[index] = (char *)NULL;
  139.     if (debug) {
  140.         printf("sending to < ");
  141.         for (i = saveindex; i < index; i++)
  142.         printf("%s ",sendargv[i]);
  143.         printf(">\n");
  144.     } else {
  145.         switch (pid = fork()) {
  146.         case -1:
  147.             done(ERR_OSERR,"pathsend: can't fork\n");
  148.         case 0:
  149.             execv(sendmail,sendargv);
  150.             done(ERR_OSFILE,"pathsend: can't execl %s\n",SENDMAIL);
  151.         default:
  152.             while (wait((int *) NULL) != pid);
  153.             break;
  154.         }
  155.     }
  156.     }
  157.  
  158.     done(0);
  159. }
  160.  
  161. done(status,str,a,b,c,d,e,f)
  162. int status;
  163. char *str;
  164. {
  165.     if (status)
  166.     fprintf(stderr,str,a,b,c,d,e,f);
  167.     exit(status);
  168. }
  169.  
  170. char *
  171. mkstring(string)
  172. char *string;
  173. {
  174.     char *str, *malloc();
  175.  
  176.     if ((str = malloc(strlen(string)+1)) == (char *)NULL)
  177.     done(ERR_SOFTWARE,"can't malloc for destination\n");
  178.     strcpy(str,string);
  179.     return(str);
  180. }
  181.  
  182. char *
  183. findhost(user,dest)
  184. char *user,*dest;
  185. {
  186.     char *newuser,*index();
  187.  
  188.     if ((newuser = index(user,'!')) == NULL)
  189.     return((char *) NULL);
  190.  
  191.     sscanf(user,"%[^!]!",dest);
  192.     return(++newuser);
  193. }
  194. @
  195.  
  196.  
  197. 1.1
  198. log
  199. @Initial revision
  200. @
  201. text
  202. @d37 2
  203. a38 2
  204.     char *dest, *user, *from = ((char *) NULL), *mkstring();
  205.     char buf[2048];
  206. d64 1
  207. a64 1
  208.         "Usage: pathsend [-dv] [-m mailer] [user@@dest-host...]\n");
  209. d86 2
  210. a87 2
  211.     user = *argv;
  212.     dest = (char *) NULL;
  213. d89 4
  214. a92 4
  215.     for (bp = *argv; *bp; *bp++) {
  216.         if (*bp == '@@')
  217.         dest = bp;
  218.     }
  219. d94 3
  220. a96 3
  221.     if (!dest)
  222.         done(ERR_DATAERR,
  223.         "pathsend: <%s> - badly formed address (no '@@')\n",*argv);
  224. d98 5
  225. a102 1
  226.     *dest++ = '\0';
  227. a103 9
  228.     sprintf(buf, "%s", dest);
  229.     key.dptr = buf;
  230.     key.dsize = strlen(buf) + 1;
  231.     content = fetch(key);
  232.  
  233.     if (content.dptr == 0)
  234.         sprintf(buf,"%s!%s",dest,user);
  235.     else
  236.         sprintf(buf, content.dptr, user);
  237. d153 13
  238. @
  239.